/*
 * TPCAnim.cpp
 *
 *  Created on: 08.10.2012
 *      Author: caffeine
 */

#include "TPCAnim.h"
#include <ISceneManager.h>

namespace irr {
namespace scene {

TPCAnim::TPCAnim(irr::scene::ISceneManager*_manager_,irr::scene::ISceneNode *_target_,irr::scene::ICameraSceneNode*_cam_){
	manager=_manager_;
	target=_target_;
	camera=_cam_;
	rotH=manager->addEmptySceneNode(target);
	rotV=manager->addEmptySceneNode(rotH);
	/*
	rotH=device->getSceneManager()->addCubeSceneNode(5,target);
	rotV=device->getSceneManager()->addCubeSceneNode(5,rotH);

	rotH->setMaterialFlag(irr::video::EMF_LIGHTING,false);
	rotH->setMaterialFlag(irr::video::EMF_WIREFRAME,true);

	rotV->setMaterialFlag(irr::video::EMF_LIGHTING,false);
	rotV->setMaterialFlag(irr::video::EMF_WIREFRAME,true);
	*/
	camera->setParent(rotV);
	setRadius(50);
	needRotH=0;
	needRotHB=false;
	minRot=1;
}

bool TPCAnim::isNeedRotate(){
	return abs(needRotH-getRotationH())>minRot;
}

void TPCAnim::rotateTo(irr::f32 r){
	needRotH=r;
	needRotHB=true;

	/*
	if (t<r)
		setRotationH(t+1);
	if (t>r)
		setRotationH(t-1);
		*/
}

void TPCAnim::animateNode(irr::scene::ISceneNode*node,irr::u32 timeMs){
	if (needRotHB){
		irr::f32 tt=getRotationH();
		irr::f32 t=needRotH-tt;
		if (abs(t)<minRot){
			setRotationH(needRotH);
			needRotHB=false;
		} else{
			if (abs(t)<20){
				if (t>0)
					setRotationH(tt+(20*1)/(timeMs*0.001));
				else
					setRotationH(tt+(-20*1)/(timeMs*0.001));
			}else
				setRotationH(tt+(t*1)/(timeMs*0.001));
		}
	}

	camera->setTarget(target->getAbsolutePosition());
}
/*
bool TPCAnim::isEventReceiverEnabled() const{
	return true;
}
*/

irr::f32 TPCAnim::getRotationH(){
	return rotH->getRotation().Y;
}

void TPCAnim::setRotationH(irr::f32 r){
	irr::core::vector3df temp=rotH->getRotation();
	temp.Y=r;
	rotH->setRotation(temp);
}

void TPCAnim::setRadius(irr::f32 r){
	radius=r;
	camera->setPosition(irr::core::vector3df(0,0,radius));
}

irr::f32 TPCAnim::getRaduis(){
	return radius;
}

bool TPCAnim::OnEvent(const irr::SEvent& event){
	if (event.EventType==irr::EET_MOUSE_INPUT_EVENT){

		if (event.MouseInput.Event==irr::EMIE_MOUSE_WHEEL){
			setRadius(getRaduis()-event.MouseInput.Wheel*2);
			return true;
		}

		if (event.MouseInput.isLeftPressed() && !mouseDown){
			mouseDown=true;
			needRotHB=false;
		}
		if (!event.MouseInput.isLeftPressed() && mouseDown)
			mouseDown=false;

		if (mouseDown){
			irr::core::vector3df temp;

			temp=rotH->getRotation();
			temp.Y-=(oldX-event.MouseInput.X)*0.2;
			rotH->setRotation(temp);

			temp=rotV->getRotation();
			temp.X+=(oldY-event.MouseInput.Y)*0.2;
			rotV->setRotation(temp);
		}
		oldX=event.MouseInput.X;
		oldY=event.MouseInput.Y;
		return true;
	}
	return false;
}

irr::scene::ISceneNodeAnimator*TPCAnim::createClone(irr::scene::ISceneNode*node,irr::scene::ISceneManager*newManager){
	return 0;
}

void TPCAnim::serializeAttributes(irr::io::IAttributes * out,irr::io::SAttributeReadWriteOptions * options ) const{
	//
}
void TPCAnim::deserializeAttributes(irr::io::IAttributes * in,irr::io::SAttributeReadWriteOptions * options ){
	//
}

} /* namespace scene */
} /* namespace irr */
